package com.ruoyi.develop.projectInvoiceInfo.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.service.IProjectBaseInfoService;
import com.ruoyi.develop.projectFundBack.domain.ProjectFundBack;
import com.ruoyi.develop.projectFundBack.service.IProjectFundBackService;
import com.ruoyi.develop.projectInvoiceInfo.domain.ProjectInvoiceInfo;
import com.ruoyi.develop.projectInvoiceInfo.mapper.ProjectInvoiceInfoMapper;
import com.ruoyi.develop.projectInvoiceInfo.service.IProjectInvoiceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 项目开票信息管理Service业务层处理
 *
 * @author qh
 * @date 2023-01-10
 */
@Service
public class ProjectInvoiceInfoServiceImpl implements IProjectInvoiceInfoService {
    @Resource
    private ProjectInvoiceInfoMapper projectInvoiceInfoMapper;
    @Autowired
    private IProjectBaseInfoService projectBaseInfoService;
    @Autowired
    private IProjectFundBackService projectFundBakService;

    /**
     * 查询项目开票信息管理
     *
     * @param id 项目开票信息管理主键
     * @return 项目开票信息管理
     */
    @Override
    public ProjectInvoiceInfo selectById(String id) {
        return projectInvoiceInfoMapper.selectById(id);
    }

    /**
     * 查询项目开票信息管理列表
     *
     * @param entity 项目开票信息管理
     * @return 项目开票信息管理
     */
    @Override
    public List<ProjectInvoiceInfo> selectList(ProjectInvoiceInfo entity) {
        return projectInvoiceInfoMapper.selectList(entity);
    }

    /**
     * 新增项目开票信息管理
     *
     * @param entity 项目开票信息管理
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public int insert(ProjectInvoiceInfo entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        int i = projectInvoiceInfoMapper.insert(entity);

        //计算项目已开发票金额
        ProjectInvoiceInfo projectFeeInfo2 = new ProjectInvoiceInfo();
        projectFeeInfo2.setProjectId(entity.getProjectId());
        List<ProjectInvoiceInfo> projectFeeInfoList = selectList(projectFeeInfo2);
        BigDecimal allMoney = BigDecimal.ZERO;
        for (ProjectInvoiceInfo projectFeeInfo1 : projectFeeInfoList) {
            if (projectFeeInfo1.getInvoiceMoney() != null) {
                allMoney = projectFeeInfo1.getInvoiceMoney().add(allMoney);
            }
        }
        ProjectBaseInfo projectBaseInfo = projectBaseInfoService.selectById(entity.getProjectId());

        projectBaseInfo.setTaxAll(allMoney);
        projectBaseInfoService.update(projectBaseInfo);
        return i;
    }

    /**
     * 修改项目开票信息管理
     *
     * @param entity 项目开票信息管理
     * @return 结果
     */
    @Override
    public int update(ProjectInvoiceInfo entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return projectInvoiceInfoMapper.update(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public int update0704(ProjectInvoiceInfo entity) {

        ProjectFundBack fundBack = entity.getProjectFundBack();
        if (fundBack.getBackMoney().equals(entity.getRemainMoney())) {
            entity.setStatus(BusinessConstants.FUND_YES);
        } else {
            entity.setStatus(BusinessConstants.FUND_PART);
        }
        entity.setBackMoney(entity.getBackMoney().add(fundBack.getBackMoney()));
        entity.setRemainMoney(entity.getRemainMoney().subtract(fundBack.getBackMoney()));
        projectFundBakService.insert(fundBack);

        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return projectInvoiceInfoMapper.update(entity);
    }

    /**
     * 批量删除项目开票信息管理
     *
     * @param ids 需要删除的项目开票信息管理主键
     * @return 结果
     */
    @Override
    @Transactional(readOnly = false)
    public int deleteByIds(String[] ids) {
        int i = projectInvoiceInfoMapper.deleteByIds(ids);
//        for(String id:ids){
        ProjectInvoiceInfo projectFeeInfo = selectById(ids[0]);
        if (projectFeeInfo != null) {
            ProjectInvoiceInfo projectFeeInfo2 = new ProjectInvoiceInfo();
            projectFeeInfo2.setProjectId(projectFeeInfo.getProjectId());
            List<ProjectInvoiceInfo> projectFeeInfoList = selectList(projectFeeInfo2);
            BigDecimal allMoney = BigDecimal.ZERO;
            for (ProjectInvoiceInfo projectFeeInfo1 : projectFeeInfoList) {
                if (projectFeeInfo1.getInvoiceMoney() != null) {
                    allMoney = projectFeeInfo1.getInvoiceMoney().add(allMoney);
                }
            }
            ProjectBaseInfo projectBaseInfo = projectBaseInfoService.selectById(projectFeeInfo.getProjectId());
            projectBaseInfo.setTaxAll(allMoney);
            projectBaseInfoService.update(projectBaseInfo);
        }
//        }
        return i;
    }

    /**
     * 删除项目开票信息管理信息
     *
     * @param id 项目开票信息管理主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return projectInvoiceInfoMapper.deleteById(id);
    }
}
